﻿namespace HynuIOTA.Web.Areas.Admin.Controllers
{
    using HynuIOTA.Web.Helpers;
    using HynuIOTA.Common.Attribute;
    using HynuIOTA.Entity;
    using HynuIOTA.Entity.FormatEntity;
    using HynuIOTA.Helpers;
    using HynuIOTA.Web.Filters;
    using HynuIOTA.Web.Models;
    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    using static HynuIOTA.Common.StringConst;


    public class ChannelController : BaseController
    {
        /// <summary>
        /// GET: Admin/Channel/ChannelList
        /// 栏目列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ChannelList()
        {
            return View();
        }

        /// <summary>
        /// GET: Admin/Channel/AddChannel
        /// 添加栏目
        /// </summary>
        /// <returns></returns>
        public ActionResult AddChannel()
        {
            return View();
        }

        /// <summary>
        /// GET: Admin/Channel/EditChannel
        /// 编辑栏目
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ActionResult> EditChannel(int? Id)
        {
            if (Id == null)
            {
                return OpContext.JsMsg(MODEL_VALIDATE_ERROR);
            }

            var channel = await OpContext.Service.Channel.Where(c => c.Id == Id.Value).FirstOrDefaultAsync();
            if (channel == null)
            {
                return OpContext.JsMsg(DATA_NOEXIST_ERROR);
            }

            ViewBag.channel = channel;
            return View();
        }

        /// <summary>
        /// ANY: Admin/Channel/GetChannelPage
        /// 获取栏目列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        [AjaxRequest]
        public async Task<ActionResult> GetChannelPage(int page = 1, int rows = int.MaxValue)
        {
            var pageData = new PageData() { PageIndex = page, PageSize = rows };
            var pageList = await (await OpContext.Service.Channel.WherePageAsync(pageData, c => c.IsDel == false, c => c.Id, true))
                    .ToListAsync();

            return OpContext.JsonMsgOK(data: new { total = pageData.RowCount, rows = pageList.Select(c => c.ToPOCO()) });
        }

        /// <summary>
        /// POST: Admin/Channel/PostChannel
        /// 添加栏目
        /// </summary>
        /// <param name="channelViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public async Task<ActionResult> PostChannel(PostChannelViewModel channelViewModel)
        {
            if (ModelState.IsValid == false)
            {
                return OpContext.JsonMsgFail(MODEL_VALIDATE_ERROR);
            }

            if (channelViewModel.ParentId != null)
            {
                if (await OpContext.Service.Channel.Where(c => c.IsDel == false && c.Id == channelViewModel.ParentId.Value).AnyAsync() == false)
                {
                    return OpContext.JsonMsgFail(DATA_NOEXIST_ERROR);
                }
            }

            var channel = new Channel()
            {
                Name = channelViewModel.Name,
                Link = channelViewModel.Link ?? "#",
                Description = channelViewModel.Description,
                IsPage = channelViewModel.IsPage,
                ParentId = channelViewModel.ParentId,
                Seq = channelViewModel.Seq,
                CreateTime = DateTime.Now,
                IsDel = false
            };
            OpContext.Service.Channel.Add(channel);
            var result = await OpContext.SaveChangesAsync();

            if (result != null)
            {
                return result;
            }
            OpContext.RefreshCacheDependency(CacheDependencyKeys.Channel);
            return OpContext.JsonMsgOK(DATA_ADD_SUCCESS);
        }

        /// <summary>
        /// PUT: Admin/Channel/PutChannel
        /// 更改栏目
        /// </summary>
        /// <param name="channelViewModel"></param>
        /// <returns></returns>
        public async Task<ActionResult> PutChannel(PutChannelViewModel channelViewModel)
        {
            if (ModelState.IsValid == false)
            {
                return OpContext.JsonMsgFail(MODEL_VALIDATE_ERROR);
            }

            var channel = await OpContext.Service.Channel
                .Where(c => c.Id == channelViewModel.Id && c.IsDel == false).FirstOrDefaultAsync();
            if (channelViewModel.ParentId != null)
            {
                var parentIsExist = await OpContext.Service.Channel
                    .Where(c => c.IsDel == false && c.Id == channelViewModel.ParentId).AnyAsync();
                if (channel == null || parentIsExist == false)
                {
                    return OpContext.JsonMsgFail(DATA_NOEXIST_ERROR);
                }

            }

            Tools.SetValues(channel,channelViewModel,ModelState.Keys.ToArray());
            var result = await OpContext.SaveChangesAsync();

            if (result != null)
            {
                return result;
            }
            OpContext.RefreshCacheDependency(CacheDependencyKeys.Channel);
            return OpContext.JsonMsgOK(DATA_MODIFY_SUCCESS);
        }

        /// <summary>
        /// ANY: Admin/Channel?GetParentChannelList
        /// 获取在下拉列表框中显示的父栏目Json数据
        /// </summary>
        /// <returns></returns>
        [AjaxRequest]
        public async Task<ActionResult> GetParentChannelList()
        {
            var channels = await OpContext.Service.Channel.Where(c => c.ParentId == null && c.IsDel == false)
                .Select(c => new ComboxJson(){id = c.Id, text = c.Name, selected = false}).ToListAsync();
            return OpContext.JsonMsgOK(data: channels);
        }

        /// <summary>
        /// ANY: Admin/Channel/GetChannelCombox
        /// 获取用于在下拉列表框显示的Json栏目数据
        /// </summary>
        /// <returns></returns>
        [AjaxRequest]
        public async Task<ActionResult> GetChannelCombox()
        {
            var channels = await OpContext.Service.Channel.Where(c => c.IsPage == false && c.IsDel == false)
                .Select(c => new ComboxJson {id = c.Id, text = c.Name, selected = false}).ToListAsync();
            return OpContext.JsonMsgOK(data: channels);
        }
    }
}